//
//  AppQuickCode.swift
//  VISUAL
//
//  Created by 刘森 on 2019/11/29.
//  Copyright © 2019 admin. All rights reserved.
//

import Foundation
import UIKit

let KDeviceUUID = UIDevice.current.identifierForVendor?.uuidString ?? ""
let KScreenWidth = UIScreen.main.bounds.width
let KScreenHeight = UIScreen.main.bounds.height
let KScreenSize = UIScreen.main.bounds.size


func Adapter(_ value: CGFloat) -> CGFloat {
    return value * KScreenWidth / 414
}

func KGetkeywindows() -> UIWindow? {
    var window:UIWindow? = nil
    if #available(iOS 13.0, *) {
        for windowScene:UIWindowScene in ((UIApplication.shared.connectedScenes as?  Set<UIWindowScene>)!) {
            if windowScene.activationState == .foregroundActive {
                window = windowScene.windows.first
                break
            }
        }
        return window
    }else{
        return UIApplication.shared.keyWindow
    }
}

func KFont(_ size: CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size)
}

func KFontBold(_ size: CGFloat) -> UIFont {
    return UIFont.boldSystemFont(ofSize: size)
}

func KColorRGBA(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat,_ alpha: CGFloat = 1.0) -> UIColor {
    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}

func KSafeAreaBottom() -> CGFloat {
    var result: CGFloat = 0
    let window: UIWindow? = (UIApplication.shared.delegate?.window ?? nil)
    if #available(iOS 11.0, *) {
        result = window?.safeAreaInsets.bottom ?? 0
    }
    return result
}

func KUserDefaults(forKey: String) -> Any? {
    return UserDefaults.standard.object(forKey: forKey)
}

func KDicValueString(_ dic:[String: Any]) ->String?{
    let data = try? JSONSerialization.data(withJSONObject: dic,options: [])
    let str = String(data: data!,encoding:String.Encoding.utf8)
    return str
}

func ImageName(_ value: String) -> UIImage {
    return UIImage(named: value) ?? UIImage()
}

func PointInSideView(point: CGPoint?, view: UIView?) -> Bool {
    guard point != nil && view != nil else {
        return false
    }
    let rect = RelativeToWindowRect(view!)
    if point!.x >= rect.origin.x && point!.x <= rect.origin.x + rect.size.width && point!.y >= rect.origin.y && point!.y <= rect.origin.y + rect.size.height {
        return true
    }
    return false
}

func RelativeToWindowRect(_ referenceView: UIView) -> CGRect{
    let window = UIApplication.shared.delegate?.window
    var rect = CGRect.zero
    if let value = window {
        rect = referenceView.convert(referenceView.bounds, to: value)
    }
    return rect
}

func DispatchAfter(after: Double, handler:@escaping ()->())
{
    DispatchQueue.main.asyncAfter(deadline: .now() + after) {
        handler()
    }
}

func DispatchMainThread(handler:@escaping ()->())
{
    DispatchQueue.main.async {
        handler()
    }
}

func KGetCurrentViewController() -> UIViewController? {
    if var window = UIApplication.shared.keyWindow {
        if window.windowLevel != .normal {
            let windows = UIApplication.shared.windows
            for tempWindow in windows where tempWindow.windowLevel == .normal {
                window = tempWindow
            }
        }
        if let frontView = window.subviews.first, let nextVC = frontView.next, nextVC.isKind(of: UIViewController.self) {
            return nextVC as? UIViewController
        } else {
            return matchSuitViewController(target: window.rootViewController)
        }
    }
    return nil
}


func matchSuitViewController(target: UIViewController?) -> UIViewController? {
    guard let resultVC = target else {
        return target
    }
    if resultVC.isKind(of: UITabBarController.self),  let tabbar = resultVC as? UITabBarController  {
        if let selectVC = tabbar.selectedViewController,
            selectVC.isKind(of: UINavigationController.self),
            let navi = selectVC as? UINavigationController {
            return navi.visibleViewController
        }
        return tabbar.selectedViewController
    }
    if resultVC.isKind(of: UINavigationController.self), let navi = resultVC as? UINavigationController  {
        return navi.visibleViewController
    }
    return resultVC
}

//

